#include "ChatServer.hpp"
#include "Route.hpp"

using task_t = std::function<void()>;

void Usage(std::string proc)
{
    std::cerr << "Usage: " << proc << " localport" << std::endl;
}
// 不需要知道sever端的ip，因为用了INADDR_ANY，默认所有端口都会打开
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(0);
    }
    uint16_t serverport = std::stoi(argv[1]);
    EnableConsoleLogStrategy();

    auto tp = ThreadPool<task_t>::GetInstance();

    std::unique_ptr<Route> r = std::make_unique<Route>(); 

    std::unique_ptr<ChatServer> server = std::make_unique<ChatServer>(serverport, 
                [&tp, &r](int sockfd, std::string message, InetAddr addr)
                {
                    task_t task = std::bind(&Route::RouteMessageToAll, r.get(), sockfd, message, addr);
                    tp->Enqueue(task);
                });
    LOG(Loglevel::DEBUG) << "server创建成功" ;
    server->Init();
    server->Start();
}